#pragma once
#include "Utils\UtilsGL.h"
#include <vector>

class Terrain;
class FrustumClass;

const int MAX_VOXEL = 600;

using namespace std;

class QuadTree
{
public:
	struct GridCell
    {
        Vector3f pos[8];
		float val[8];
    };
	struct NodeType
	{
        float positionX, positionY, positionZ, width;
		int voxelCount;
		float* m_pGridData;
		Vector3i dataSize;
		//vector<Vector3f> m_pVertexList;
		//vector<int> m_pIndicesist;
		//GLuint m_pVertexBuffer;
		//GLuint m_pIndicesBuffer;
        NodeType* nodes[8];
		BoundingBox m_AABB;
	};

	QuadTree();
	/*
	* Destroys the QuadTree object.
	*/
	virtual ~QuadTree(void);

	void Create(Terrain* terrain);
	void Tick();
	void Draw();

private:
	void RenderMarchCube(NodeType* node);
	int Polygonise(GridCell &grid, float isolevel, Vector3f *triangles);
	void CreateTreeNode(NodeType* node, float positionX, float positionY, float positionZ, float width, Vector3i datasize);
	int CountVoxels(float positionX, float positionZ, float positionY, float width);
	bool IsGridContained(GridCell& cell, int positionX, int positionY, int positionZ, float width);
	bool IsVoxelContained(Vector3f &voxel, float positionX, float positionY, float positionZ, float width);
	Vector3f VertexInterp(float isolevel, Vector3f p1, Vector3f p2, float valp1, float valp2);
	void RenderNode(NodeType* node);
	int getal;
	Vector3f m_VoxelCount;
	Terrain* m_pTerrainPtr;
	NodeType* m_parentNode;
	Vector3f m_Postion;
	QuadTree(const QuadTree& t);
	QuadTree& operator=(const QuadTree& t);
};
